package controller.menu;

import controller.Operation.*;
import model.NormalUser;
import view.menu.NormalUserMenuView;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.ParseException;

/**
 * @Question NormalUserMenuView
 * @Date 2022/12/17 20:00
 * @Solution
 */
public class NormalUserMenu implements ActionListener {
    public NormalUserMenu(NormalUser user) {
        this.user = user;
    }

    private NormalUser user;
    private JButton showAllButton;
    private DefaultTableModel bookModel;
    private JTable bookTable;
    private JTable recordTable;
    private DefaultTableModel recordModel;
    private JButton showAllBookButton;
    private JButton searchBookButton;
    private JRadioButton isIn;
    private JTextField bookId;
    private JTextField bookName;
    private JTextField bookAuthor;
    private JTextField bookPress;
    private JPanel bookContainer;
    private JButton bookBorrowButton;
    private JButton bookRenewButton;
    private JButton bookReturnButton;
    public void init() {
        NormalUserMenuView menu = new NormalUserMenuView();
        menu.setVisible(true);
        //获取组件
        showAllButton=menu.getBookShow();
        bookModel=menu.getBookModel();
        //获取各组件
        bookTable=menu.getBookTable();
        recordModel=menu.getRecordModel();
        recordTable=menu.getRecordTable();
        showAllBookButton=menu.getBookShow();
        searchBookButton=menu.getBookSearch();
        bookBorrowButton = menu.getBookBorrow();
        isIn=menu.getNoOut();
        bookId=menu.getBookId();
        bookName=menu.getBookName();
        bookAuthor=menu.getBookAuthor();
        bookPress=menu.getBookPre();
        bookContainer=menu.getBookMenu();
        bookRenewButton=menu.getBookRenew();
        bookReturnButton=menu.getBookReturn();
        //初始化信息，显示个人全部
        bookModel=menu.getBookModel();
        recordModel=menu.getRecordModel();
        Show.showAllBook(bookModel);
        Show.showByUserIdRecord(recordModel, user.getId());
        menu.setVisible(true);
        //添加监视器
        showAllButton.addActionListener(this);
        searchBookButton.addActionListener(this);
        bookBorrowButton.addActionListener(this);
        bookRenewButton.addActionListener(this);
        bookReturnButton.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource()==showAllButton) {
            Show.showAllBook(bookModel);
        } else if(e.getSource()==searchBookButton) {
            Search.bookSearch(bookContainer,bookModel,bookTable,bookId,bookName,bookAuthor,bookPress,isIn);
        } else if(e.getSource()==bookBorrowButton) {
            Borrow.bookBorrow(user,bookTable,bookModel,recordModel);
        } else if(e.getSource()==bookRenewButton) {
            try {
                Renew.bookRenew(user,bookTable,bookModel,recordModel,recordTable);
            } catch (ParseException ex) {
                ex.printStackTrace();
            }
        } else if(e.getSource()==bookReturnButton){
            try {
                ReturnBook.returnBook(user,bookTable,bookModel,recordModel,recordTable);
            } catch (ParseException ex) {
                ex.printStackTrace();
            }
        }
    }
}
